package 
{
	import com.swfwire.decompiler.AsyncSWFReader;
	import com.swfwire.decompiler.AsyncSWFWriter;
	import com.swfwire.decompiler.SWF10Reader;
	import com.swfwire.decompiler.SWF10Writer;
	import com.swfwire.decompiler.SWFByteArray;
	import com.swfwire.decompiler.SWFReadResult;
	import com.swfwire.decompiler.SWFReader;
	import com.swfwire.decompiler.SWFWriteResult;
	import com.swfwire.decompiler.SWFWriter;
	import com.swfwire.decompiler.data.swf.SWF;
	import com.swfwire.decompiler.events.AsyncSWFReaderEvent;
	import com.swfwire.decompiler.events.AsyncSWFWriterEvent;
	
	import crl.encrypt.EncryptFactory;
	import crl.encrypt.ext.ExAsyncSWFReader;
	import crl.encrypt.ext.ExAsyncSWFWriter;
	import crl.encrypt.ext.ExportSWFReader;
	
	import flash.display.Sprite;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	/**
	 * 
	 * @author crl
	 * 
	 */	
	public class EncryptApp extends Sprite//给发布用为SWF用;
	{
		public static var logHandle:*;
		private var igoreFacory:EncryptFactory
		public var playerClasses:Array;
		public function EncryptApp()
		{
			igoreFacory=new EncryptFactory();
			this.mouseChildren=this.mouseEnabled=false;
		}
		
		private var fileName:String;
		public function start(bytes:ByteArray,name:String,logHandle:Function):void{
			EncryptApp.logHandle=logHandle;
			fileName=name;
			
			var swf:SWFByteArray=new SWFByteArray(bytes);
			
			
			encode(swf,new ExportSWFReader());
			
			//encode(swf,new ExAsyncSWFReader());
		}
		
		private function encode(bytes:SWFByteArray,reader:AsyncSWFReader):void{
			reader.addEventListener(AsyncSWFReaderEvent.READ_COMPLETE, readerCompleteHandler);
			reader.read(bytes);
		}
		private function readerCompleteHandler(event:AsyncSWFReaderEvent):void{
			var reader:AsyncSWFReader=event.target as AsyncSWFReader;
			reader.removeEventListener(AsyncSWFReaderEvent.READ_COMPLETE, readerCompleteHandler);
			
			var swf:SWF=event.result.swf;
			//igoreFacory.start(swf,fileName.replace(".swf",""));
			
			//return;
			var writer:AsyncSWFWriter = new ExAsyncSWFWriter();
			writer.addEventListener(AsyncSWFWriterEvent.WRITE_COMPLETE, writerCompleteHandler);
			writer.write(swf);
			
		}
		
		private function writerCompleteHandler(event:AsyncSWFWriterEvent):void{
			var writer:AsyncSWFWriter=event.target as AsyncSWFWriter;
			writer.removeEventListener(AsyncSWFWriterEvent.WRITE_COMPLETE, writerCompleteHandler);
			writeFile(event.result.bytes,fileName);
		}
		
		private function writeFile(bytes:ByteArray,name:String):void{
			
			var fileRef:FileReference=new FileReference();
			fileRef.save(bytes,"en_"+name);
		}
	}
}